#include "CPointLight.h"

CPointLight::CPointLight(const CVector3f& center, const CColor& color, const CVector3f& attenuation):
		m_center(center), m_color(color), m_attenuation(attenuation)
{
	m_type = ePointLight;	
}

CPointLight::~CPointLight(){}

CColor CPointLight::computeColor(const CRay& /*ray*/, float dist)const
{
	//if no attenuation
	if(m_attenuation.norme2() == 0)
		return m_color;
	else//calculate attenuation
		return m_color / (m_attenuation.x + m_attenuation.y * dist + m_attenuation.z * dist * dist);
}

void CPointLight::computeShadowRay(const CVector3f& point, CRay& shadowRay, float* distance)const
{
	//get a ray from the object to the light
	//CVector3f L = m_center - point; //vector from the point of the object to the light
	CVector3f L = m_viewCenter - point;
	*distance = L.norme();
	L.normalize();

	//to avoid intersection with itslef
	shadowRay.setOrigin(point + (L * 0.01f));
	shadowRay.setDirection(L);
}

void CPointLight::applyViewMatrix(const CMatrix& view)
{
	//m_center = m_center * view;
	m_viewCenter = m_center * view;
}

void CPointLight::setPosition(const CVector3f& position)
{
	m_center = position;
}